package com.market.service.impl;

import com.github.pagehelper.PageHelper;
import com.market.common.exception.ServiceException;
import com.market.common.vo.PurchaseVo;
import com.market.common.vo.ResponseCode;
import com.market.entity.*;
import com.market.mapper.*;
import com.market.service.PurchaseService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class PurchaseServiceImpl implements PurchaseService {
    @Resource
    PurchaseMapper purchaseMapper;
    @Resource
    ShopMapper shopMapper;
    @Resource
    WorkerMapper workerMapper;
    @Resource
    CommercialSupplierMapper commercialSupplierMapper;
    @Resource
    CommodityMapper commodityMapper;
    @Resource
    CommercialTenantMapper commercialTenantMapper;

    /**
     * 新增采购单
     *
     * @param purchase
     * @return
     * @throws ServiceException
     */
    @Override
    public int addPurchase(Purchase purchase) throws ServiceException {
        boolean shopFlag = false;
        boolean wirkerFlag = false;
        boolean sctFlag = false;
        boolean commodityFlag = false;
        boolean shopNameFlag = false;
        /**
         * 判断门店是否存在
         */
        List<Shop> shops = shopMapper.selectName();
        for (Shop shop : shops) {
            if (shop.getShopName().equals(purchase.getShopName())) {
                shopFlag = true;
            }
        }
        /**
         * 判断员工谁否存在
         */
        List<Worker> workers = workerMapper.selectId();
        for (Worker worker : workers) {
            if (worker.getWorkerId().equals(purchase.getPurchaseEmployeeId())) {
                wirkerFlag = true;
            }
        }
        /**
         * 判断供应商是否存在
         */
        List<CommercialSupplier> commercialSuppliers = commercialSupplierMapper.selectName();
        for (CommercialSupplier commercialSupplier : commercialSuppliers) {
            if (commercialSupplier.getSupplierCommercialTenantName().equals(purchase.getSupplierCommercialTenantName())) {
                sctFlag = true;
            }
        }
        /**
         * 判断商品是否存在
         */
        List<Commodity> selects = commodityMapper.selectId();
        for (Commodity commodity : selects) {
            if (commodity.getCommodityId().equals(purchase.getCommodityId())) {
                commodityFlag = true;
            }
        }
        /**
         * 判断员工操作门店是否与其相同
         */
        Worker worker = workerMapper.selectShopName(purchase.getPurchaseEmployeeId());
        if (worker.getCurdShop().equals(purchase.getShopName())) {
            shopNameFlag = true;
        }


        if (shopFlag && wirkerFlag && sctFlag && commodityFlag && shopNameFlag) {
            int insert = purchaseMapper.insert(purchase);
            if (insert > 0) {
                throw new ServiceException(ResponseCode.SUCCESS);
            } else {
                throw new ServiceException(ResponseCode.ERROR);
            }
        } else {
            throw new ServiceException(ResponseCode.ERROR);
        }
    }

    /**
     * 审核采购单
     *
     * @return
     * @throws ServiceException
     */
    @Override
    public List<PurchaseVo> findPurchase() throws ServiceException {
        List<PurchaseVo> selects = purchaseMapper.selects();
        for (PurchaseVo purchaseVo : selects) {
            Purchase purchase = purchaseMapper.selectAuditStatus(purchaseVo.getPurchaseId());
            switch (purchase.getAuditStatus()) {
                case 0:
                    purchaseVo.setAuditStatus("待审核");
                    break;
                case 1:
                    purchaseVo.setAuditStatus("审核通过");
                    break;
                case 2:
                    purchaseVo.setAuditStatus("审核失败");
                    break;
                default:
                    throw new ServiceException(ResponseCode.ERROR);
            }
        }
        return selects;
    }

    /**
     * 删除采购单
     *
     * @param purchaseId
     * @return
     * @throws ServiceException
     */
    @Override
    public int delPurchase(Long purchaseId) throws ServiceException {
        int delect = purchaseMapper.delect(purchaseId);
        if (delect > 0) {
            throw new ServiceException(ResponseCode.SUCCESS);
        } else {
            throw new ServiceException(ResponseCode.ERROR);
        }
    }

    /**
     * 更改采购单
     *
     * @param purchase
     * @return
     * @throws ServiceException
     */
    @Override
    public int updatePurchase(Purchase purchase) throws ServiceException {
        int update = purchaseMapper.update(purchase);
        if (update > 0) {
            throw new ServiceException(ResponseCode.SUCCESS);
        } else {
            throw new ServiceException(ResponseCode.ERROR);
        }
    }

    /**
     * 审核通过采购单
     *
     * @return
     * @throws ServiceException
     */
    @Override
    public List<PurchaseVo> findPurchasePass() throws ServiceException {
        List<PurchaseVo> purchaseVos = purchaseMapper.selectsPass();
        for (PurchaseVo purchaseVo : purchaseVos) {
            Purchase purchase = purchaseMapper.selectInStatus(purchaseVo.getPurchaseId());
            switch (purchase.getInStatus()) {
                case 0:
                    purchaseVo.setInStatus("待入库");
                    break;

                case 1:
                    purchaseVo.setInStatus("已入库");
                    break;

                case 2:
                    purchaseVo.setInStatus("已作废");
                    break;

                case 3:
                    purchaseVo.setInStatus("部分入库");
                    break;
                default:
                    throw new ServiceException(ResponseCode.ERROR);
            }
        }
        return purchaseVos;
    }

    /**
     * 搜索
     *
     * @param purchase
     * @return
     */
    @Override
    public List<PurchaseVo> selectPurchases(Purchase purchase) throws ServiceException {
        boolean shopFlag = false;
        boolean wirkerFlag = false;
        boolean sctFlag = false;
        boolean shopNameFlag = false;
        boolean tenantNameFlag = false;
        /**
         * 判断门店是否存在
         */
        List<Shop> shops = shopMapper.selectName();
        for (Shop shop : shops) {
            if (shop.getShopName().equals(purchase.getShopName())) {
                shopFlag = true;
            }
        }
        /**
         * 判断员工谁否存在
         */
        List<Worker> workers = workerMapper.selectId();
        for (Worker worker : workers) {
            if (worker.getWorkerId().equals(purchase.getPurchaseEmployeeId())) {
                wirkerFlag = true;
            }
        }
        /**
         * 判断供应商是否存在
         */
        List<CommercialSupplier> commercialSuppliers = commercialSupplierMapper.selectName();
        for (CommercialSupplier commercialSupplier : commercialSuppliers) {
            if (commercialSupplier.getSupplierCommercialTenantName().equals(purchase.getSupplierCommercialTenantName())) {
                sctFlag = true;
            }
        }
        /**
         * 判断员工操作门店是否与其相同
         */
        Worker worker = workerMapper.selectShopName(purchase.getPurchaseEmployeeId());
        if (worker.getCurdShop().equals(purchase.getShopName())) {
            shopNameFlag = true;
        }
        /**
         * 判断商户是否存在
         */
        List<CommercialTenant> select = commercialTenantMapper.select();
        for (CommercialTenant commercialTenant : select) {
            if (commercialTenant.getCommercialTenantName().equals(purchase.getSupplierCommercialTenantName())) {
                tenantNameFlag = true;
            }
        }
        if (shopFlag && wirkerFlag && sctFlag && shopNameFlag && tenantNameFlag) {
            List<PurchaseVo> purchaseVos = purchaseMapper.selectPurchases(purchase);
            if (purchaseVos.size() > 0) {
                return purchaseVos;
            } else {
                throw new ServiceException(ResponseCode.ERROR);
            }
        } else {
            throw new ServiceException(ResponseCode.ERROR);
        }
    }

    /**
     * 审核状态
     *
     * @param id
     * @return
     */
    @Override
    public Purchase findAuditStatus(Long id) throws ServiceException {
        Purchase purchase = purchaseMapper.selectAuditStatus(id);
        if (purchase != null) {
            return purchase;
        } else {
            throw new ServiceException(ResponseCode.ERROR);
        }
    }

    /**
     * 入库状态
     *
     * @param id
     * @return
     */
    @Override
    public Purchase findInStatus(Long id) throws ServiceException {
        Purchase purchase = purchaseMapper.selectInStatus(id);
        if (purchase != null) {
            return purchase;
        } else {
            throw new ServiceException(ResponseCode.ERROR);
        }
    }

/*    @Override
    public List<PurchaseVo> flindPurchases(Purchase purchase,Integer page,Integer size) throws ServiceException {
        PageHelper.startPage(page, size);
        List<PurchaseVo> purchaseVos = purchaseMapper.selectPurchases(purchase);
        if (purchaseVos.size() > 0) {
            return purchaseVos;
        } else {
            throw new ServiceException(ResponseCode.ERROR);
        }
    }*/
}
